package cn.itcast.weixin;

import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.UserService;
import cn.itcast.utils.HttpUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class WeixinLogin {
    @Autowired
    private UserService userService;
    @Autowired
    protected HttpServletRequest request;
    @RequestMapping
    public String weixinlogin(String code) {
        Map<String, Object> stringObjectMap = HttpUtils.sendGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx3bdb1192c22883f3&secret=db9d6b88821df403e5ff11742e799105&code=" + code + "&grant_type=authorization_code");
        String openid = (String) stringObjectMap.get("openid");
        User user = userService.findOpenId(openid);
        if (user == null) {
            request.setAttribute("openId", openid);
            return "forward:/rengzh.jsp";
        }
        request.setAttribute("user", user);
        return "forward:/weixinlgin.jsp";
    }
}
